package org.hard.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
//import io.seata.spring.annotation.GlobalTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import org.hard.entity.FileInfo;
import org.hard.mapper.FileInfoMapper;
import org.hard.service.FileInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author: 王林博
 * @date: 2022年08月09日 17:55
 */
@Data
@RestController
public class HelloTestController {

    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private FileInfoMapper fileInfoMapper;



    //    @HystrixCommand(
//            groupKey = "produce-server-list-pool",//服务名称，相同名称使用同一个线程池
//            commandKey = "getListFallback",//接口名称，默认方法名
//            threadPoolKey = "produce-server-list-pool",//线程池名称，相同名称使用同一个线程池
//            commandProperties = {
//                    //超时时间，默认 1000 ms
//                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,
//                            value = "2000")
//            },
//            threadPoolProperties = {
//                    @HystrixProperty(name = HystrixPropertiesManager.CORE_SIZE,value = "10"),//线程池大小
//                    @HystrixProperty(name = HystrixPropertiesManager.MAX_QUEUE_SIZE,value = "10"),//最大队列阈值，默认-1
//                    @HystrixProperty(name = HystrixPropertiesManager.KEEP_ALIVE_TIME_MINUTES,value = "3"),//线程存活时间，默认1分钟
//                    @HystrixProperty(name = HystrixPropertiesManager.QUEUE_SIZE_REJECTION_THRESHOLD,value = "10")//超出队列等待阈值拒绝策略
//            },
//            fallbackMethod = "getListFallback"  //服务容错回调函数
//    )
    @GetMapping("hello/{name}")
    public String hello(@PathVariable("name") String name) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(name);
        return name;
    }

    @GetMapping("getListFallback")
    public String getListFallback() {
        System.out.println("cuo");
        return "cuo";
    }

    @GetMapping("save")
//    @GlobalTransactional
    public void saveFileInfo() {
        fileInfoService.save();
    }


    @GetMapping("get")
    public List<FileInfo> getFileInfo() {

        return fileInfoMapper.selectList(new QueryWrapper<FileInfo>().lambda().eq(FileInfo::getId,1796457316828614658L));
    }


    public static void main(String[] args) throws Exception {
//        String url = "classpath:sensitiveWords.txt";
//        final String replace = url.replace("classpath:", "");
//        System.out.println(replace);

        List<String> strings = new ArrayList<>();
        strings.add("qwe");
        strings.add("qwe1");
        strings.add("qwe2");
        String itemList = strings.stream().collect(Collectors.joining(","));
        System.out.println(Arrays.toString(strings.toArray()));
//        TuputechContentUtil.sendContentVerify(itemList.split(","));
//        System.out.println(HelloTestController.class.getClassLoader().getResource("redisson.yml"));

//        List<String> strings = new ArrayList<>();
//        System.out.println(CollectionUtil.isNotEmpty(strings));
//        strings.add(null);
//        System.out.println(CollectionUtil.isNotEmpty(strings));
//        System.out.println(Integer.valueOf("00:15"));
//        System.out.println(  < "10:05");


//        LocalDateTime startTime = LocalDateTime.now();
//        LocalDateTime endTime = LocalDateTime.now();
////
//        String[] start = "10:00".split(":");
//        LocalDateTime banStartTime = startTime.withHour(Integer.parseInt(start[0])).withMinute(Integer.parseInt(start[1]));
//
//        String[] end = "10:59".split(":");
//        LocalDateTime banEndTime = endTime.withHour(Integer.parseInt(end[0])).withMinute(Integer.parseInt(end[1]));
//        System.out.println(banStartTime.isBefore(banEndTime));
//
//        LocalDateTime localDateTime = LocalDateTime.now();
//        System.out.println(banStartTime);
//        System.out.println(banEndTime);
//        System.out.println(localDateTime);
//        System.out.println(localDateTime.isAfter(banEndTime));
//        boolean b = localDateTime.isAfter(banStartTime) && localDateTime.isBefore(banEndTime);
//        System.out.println(b);
//        Calendar instance = Calendar.getInstance();
//        // 获取今天星期几
//        int day = instance.get(Calendar.DAY_OF_WEEK);
//        System.out.println(day);
//        FileInfo info = new FileInfo();
//        FileInfo info1;
//
//        BeanUtils.copyProperties(info, null);
//
//        List<String> list = new ArrayList<>();
//        list.add("ser");
//        list.add("ser2");
//        list.add("s1er2");
//        final String collect = list.stream().collect(Collectors.joining(","));
//        System.out.println(collect);
//        System.out.println(System.currentTimeMillis() / 1000 - 10 * 60);

//        List<Integer> nums = new ArrayList<>();
//        System.out.println(nums);
//        List<FileInfo> fileInfos = new ArrayList<>();
//        List<FileInfo> collect = fileInfos.stream().sorted(Comparator.comparing(FileInfo::getId).reversed()).collect(Collectors.toList());
//        System.out.println(collect.size());
//        System.out.println(collect);
//        System.out.println(DateUtil.date(1682100000000L));
//        final int compare = DateUtil.compare(DateUtil.date(1682100000*1000L), DateUtil.date());
//        System.out.println( compare);
//        System.out.println(Math.toIntExact(DateUtil.offsetDay(new DateTime(), -15).getTime() / 1000));
//        System.out.println(getInterIP2());
//        System.out.println(getOutIPV4());
//        System.out.println(getIP());
//        System.out.println(UUID.randomUUID().toString());
//        System.out.println(UUID.fastUUID());
//        System.out.println(UUID.randomUUID(false));
//        System.out.println(HelloTestController.class.getResource("/redisson.yml").getPath());
//        LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);
//        LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);
//        String[] start = "00:00".split(":");
//        LocalDateTime banStartTime = startTime.withHour(Integer.parseInt(start[0])).withMinute(Integer.parseInt(start[1]));
//
//        String[] end = "00:00".split(":");
//        LocalDateTime banEndTime = endTime.withHour(Integer.parseInt(end[0])).withMinute(Integer.parseInt(end[1]));
//
//        LocalDateTime localDateTime = LocalDateTime.now();
//
//        System.out.println(localDateTime.isAfter(banStartTime) && banEndTime.isAfter(localDateTime));

//        Calendar date = Calendar.getInstance();
//        date.setTime(nowTime);
//        Calendar begin = Calendar.getInstance();
//        begin.setTime(beginTime);
//        Calendar end = Calendar.getInstance();
//        end.setTime(endTime);
//        return date.after(begin) && date.before(end);
    }

    /**
     * 根据域名获取ip地址
     */
    public static String getIfIP() {
        String toIp = "";
        try {
            String pathName = "baidu.com";//这里没有进行支持https协议的，可以自行进行切割字符串
            final InetAddress[] allByName = InetAddress.getAllByName(pathName);
            InetAddress address = allByName[0];
            toIp = address.getHostAddress();
        } catch (Exception e) {
            toIp = "";
        }
        return toIp;
    }


    public static String getInterIP1() throws Exception {
        return InetAddress.getLocalHost().getHostAddress();
    }

    public static String getInterIP2() throws SocketException {
        String localip = null;// 本地IP
        String netip = null;// 外网IP
        Enumeration<NetworkInterface> netInterfaces;
        netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        boolean finded = false;// 是否找到外网IP
        while (netInterfaces.hasMoreElements() && !finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                    // 外网IP
                    netip = ip.getHostAddress();
                    finded = true;
                    break;
                } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                    // 内网IP
                    localip = ip.getHostAddress();
                }
            }
        }
        if (netip != null && !"".equals(netip)) {
            return netip;
        } else {
            return localip;
        }
    }

    public static String getOutIPV4() {
        String ip = "";
        String chinaz = "http://ip.chinaz.com";

        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
        }
        return ip;
    }

    public static String getIP() {
        String ip = "http://pv.sohu.com/cityjson?ie=utf-8";

        String inputLine = "";
        String read = "";
        String toIp = "";
        try {
            URL url = new URL(ip);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            while ((read = in.readLine()) != null) {
                inputLine += read;
            }
            String ObjJson = inputLine.substring(inputLine.indexOf("=") + 1, inputLine.length() - 1);
//            System.out.println(ObjJson);
            final JSON parse = JSONUtil.parse(ObjJson);
            final Object cip = parse.getByPath("cip");
//            JSONObject jsonObj= JSONObject.parseObject(ObjJson);
            toIp = (String) cip;

//            throw new Exception();
        } catch (Exception e) {
            toIp = "";
        }
        return toIp;
    }
}
